<?php

namespace app\admin\controller;

use app\admin\controller\common\Base;

class Power extends Base
{
    public function index()
    {
        return $this->fetch();
    }
    public function add()
    {
        return $this->fetch();
    }
    public function init()
    { //查询权限节点

        $menuinfo = db('menuinfo')->field('id,title,icon,href,is_menu,sort,pid,target')->order('sort desc')->where(['is_del' => 0])->select();
        $data['data'] = $menuinfo;
        $data['msg'] = '';
        $data['count'] = count($menuinfo);
        return $data;
    }
    public function init2()
    {
        //查询权限节点
        $id = input('post.id');
        $menuinfo = db('menuinfo')->field('id,title,icon,href,is_menu,sort,pid,target')->where(['is_menu' => 1, 'is_del' => 0,  'pid' => -1, 'id' => ['neq', $id]])->order('sort desc')->select();
        if (!empty($menuinfo)) {
            foreach ($menuinfo as $k => $v) {
                $child = db('menuinfo')->where(['is_del' => 0, 'is_menu' => 1, 'pid' => $v['id'], 'id' => ['neq', $id]])->order('sort desc')->select();
                if ($child) {
                    $v['child'] =  $child;
                    foreach ($v['child'] as $k2 => $v2) {
                        $child2 = db('menuinfo')->where(['is_del' => 0, 'is_menu' => 1, 'pid' => $v2['id'], 'id' => ['neq', $id]])->order('sort desc')->select();
                        if ($child2) {
                            $v2['child'] = $child2;
                        }
                        $v['child'][$k2] = $v2;
                    }
                }
                $menuinfo[$k] = $v;
            }
        }
        $data['data'] = $menuinfo;
        $data['msg'] = '';
        $data['count'] = count($menuinfo);
        return $data;
    }
    //添加权限
    public function addvalue()
    {
        $value = input('post.');
        $add['icon'] = 'fa ' . $value['icon'];
        $add['inTime'] = $add['upTime'] = time();
        $add['title'] = $value['title'];
        $add['href'] = strtolower($value['href']);
        $add['target'] = $value['target'];
        $add['is_menu'] = $value['is_menu'];
        $add['sort'] = $value['sort'];
        $add['pid'] = $value['pid'];
        $res = db('menuinfo')->insert($add);
        return ['code' => $res];
    }
    //删除权限
    public function del()
    {
        $id = input('post.id');
        //删除判断顶级子集
        $res = db('menuinfo')->where(['id' => $id])->update(['is_del' => 1, 'upTime' => time()]);
        return ['code' => $res];
    }
    //查找单条权限
    public function find()
    {
        $id = input('post.id');
        $res = db('menuinfo')->where(['id' => $id])->find();
        if ($res) {
            return ['code' => 1, 'data' => $res];
        } else {
            return ['code' => 0];
        }
    }
    //修改数据
    public function edit()
    {
        $value = input('post.');
        $save['icon'] = 'fa ' . $value['icon'];
        $save['upTime'] = time();
        $save['title'] = $value['title'];
        $save['href'] = strtolower($value['href']);
        $save['target'] = $value['target'];
        $save['is_menu'] = $value['is_menu'];
        $save['sort'] = $value['sort'];
        $save['pid'] = $value['pid'];
        $save['id'] = $value['id'];
        $res = db('menuinfo')->update($save);
        return ['code' => $res];
    }
}
